package logHandle;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;

import logHandle.model.LogstashProperty;

/**
 * 
 * @since JDK 1.8
 * @version
 * @author 李萌
 * @Email limeng32@chinaunicom.cn
 * @date 2017年11月6日 下午2:42:15 ;
 * @ClassName FetchConfig ;
 *
 * @Description ADDFUNCTION ;
 * @Reason ADDREASON;
 *
 */
public class FetchConfig {

	protected static final String MAIN_XML_FILE = "log4j2.xml";

	protected static final String LOGSTASH_IP_FILE = "logstash.properties";

	protected static final String SOCKET_NAME = "logstash";

	private static final String PATTERN_NAME = "pattern";

	private static final String TARGET_NAME = "target";

	/**
	 * 和规定匹配的PatternLayout输出格式
	 */
	protected static final String patternDefault = "%date{yyyy-MM-dd HH:mm:ss,SSS} %level [%threadName] %message %n";
	// protected static final String pattern2 = "%d{yyyy-MM-dd HH:mm:ss,SSS} %p
	// [%t] %c : %m %n";

	/**
	 * 获取 path/log4j2.xml组成的全路径
	 * 
	 * @param path
	 * @return
	 */
	protected static String getXmlPath(String path) {
		return path + "/" + MAIN_XML_FILE;
	}

	/**
	 * 获取path/logstash.properties组成的全路径
	 * 
	 * @param path
	 * @return
	 */
	protected static String getLogstashPath(String path) {
		return path + "/" + LOGSTASH_IP_FILE;
	}

	/**
	 * 获取path指向的properties文件中的target属性对应的值
	 * 
	 * @param path
	 * @return
	 */
	protected static LogstashProperty fetchLogstashProperty(String path) {
		Properties prop = new Properties();
		try (InputStream in = new BufferedInputStream(
				new FileInputStream(path))) {
			prop.load(in);
			return new LogstashProperty(prop.getProperty(TARGET_NAME, ""),
					prop.getProperty(PATTERN_NAME, null));
		} catch (IOException e) {
			return new LogstashProperty("", null);
		}
	}

	/**
	 * 获取环境变量LOG4J2_XML_PATH执行的路径+/logstash.properties指向的文件中的target属性的值，该属性值应该是用,分割的一组值<br />
	 * 每个值应该是host:port格式或者host:startPort-endPort的格式
	 * 
	 * @return
	 */
	protected static LogstashProperty fetchLogstashProperty4() {
		// logstash.properties
		String path = GetEnv.getLog4J2Path();
		if (path == null || "".equals(path.trim())) {
			return null;
		} else {
			return fetchLogstashProperty(getLogstashPath(path));
		}
	}

	/**
	 * 获取./logstash.properties指向的文件中的target属性的值，该属性值应该是用,分割的一组值<br />
	 * 每个值应该是host:port格式或者host:startPort-endPort的格式
	 * 
	 * 
	 * @return
	 */
	protected static LogstashProperty fetchLogstashProperty5() {
		return fetchLogstashProperty(getLogstashPath("."));
	}

	/**
	 * 将str转换成一个list<Entry>
	 * 
	 * @param str
	 *            用,分割的，每个值是host:port格式或者host:startPort-endPort的格式的值
	 * @return
	 */
	protected static List<Entry<String, Integer>> fetchLogstashProperty6(
			String str) {
		String[] configArray = str.split(",");
		List<Entry<String, Integer>> ret = new ArrayList<>();
		for (String s : configArray) {
			if (s.indexOf(':') != -1) {
				try {
					String host = s.substring(0, s.indexOf(':'));
					switch (host) {
					case "":
						break;
					default:
						fetchLogstashProperty7(s, host, ret);
					}
				} catch (java.lang.NumberFormatException e) {
				}
			}
		}
		return ret;
	}

	/**
	 * 将s转换成entry的格式并放入retList中
	 * 
	 * @param s
	 *            host:port格式或者host:startPort-endPort的格式的值
	 * @param host
	 *            s中的host的值
	 * @param retList
	 *            包含结果的返回值
	 */
	private static void fetchLogstashProperty7(String s, String host,
			List<Entry<String, Integer>> retList) {
		if (retList == null) {
			return;
		}
		String portStr = s.substring(s.indexOf(':') + 1, s.length());
		int index = portStr.indexOf('-');
		if (index == -1) {
			Integer ip = Integer.parseInt(portStr);
			Entry<String, Integer> e = new AbstractMap.SimpleEntry<>(host, ip);
			retList.add(e);
		} else {
			String headerStr = portStr.substring(0, index);
			Integer header = Integer.parseInt(headerStr);
			String tailerStr = portStr.substring(index + 1, portStr.length());
			Integer tailer = Integer.parseInt(tailerStr);
			if (tailer < header) {
				int tmpInt = header;
				header = tailer;
				tailer = tmpInt;
			}
			for (int i = header; i <= tailer; i++) {
				Entry<String, Integer> e = new AbstractMap.SimpleEntry<>(host,
						i);
				retList.add(e);
			}
		}
	}
}
